package GUI;


import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JButton;

import client.ClientConsole;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;

/**this class get copy to remove from db*/
public class GUI_DeleteCopy extends JPanel
{
	/**clc instance of ClientConsole*/
	private ClientConsole clc;
	/**list ArrayList of results from server*/
	private ArrayList<String[]> list;
	/**table JTable to display results*/
	private JTable table;
	/**textField to get copy id to remove from db*/
	private JTextField textField;
	
	/**@param cc current ClientConsole
	 * @param gl instance of current GUI_LogIn
	 * @param gc instance of current GUI_Catalog*/
	public GUI_DeleteCopy(ClientConsole cc,final GUI_LogIn gl,final GUI_Catalog gc) {
		setLayout(null);
		clc=cc;
		this.setSize(700, 600);
		this.setName("Delete Book Copy");
		
		JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.setContentPane(gc);
			}
		});
		btnBack.setBounds(434, 422, 89, 23);
		add(btnBack);
		
		JLabel lblChooseCopyTo = new JLabel("Choose copy to remove from catalog:");
		lblChooseCopyTo.setBounds(41, 41, 300, 14);
		add(lblChooseCopyTo);
		
		showCopies();
		
		JLabel lblCopyId = new JLabel("Copy id to remove:");
		lblCopyId.setBounds(41, 352, 146, 14);
		add(lblCopyId);
		
		textField = new JTextField();
		textField.setBounds(219, 349, 86, 20);
		add(textField);
		textField.setColumns(10);
		
		JButton btnDeleteCopy = new JButton("Delete Copy");
		btnDeleteCopy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					String[] s={"deleteCopy",textField.getText()};
					if(s[1].equals(""))
					{
						JOptionPane.showMessageDialog(new JFrame(),"Please enter copy ID");
						return;
					}
					clc.client.sendToServer(s);
					while(clc.client.flag==0);
					clc.client.flag=0;
					JOptionPane.showMessageDialog(new JFrame(),clc.client.message);
					textField.setText("");
					showCopies();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnDeleteCopy.setBounds(337, 348, 132, 23);
		add(btnDeleteCopy);
		
		JButton btnLogOut = new JButton("Log Out");
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				gl.LogOut();
			}
		});

		btnLogOut.setBounds(560, 422, 89, 23);
		add(btnLogOut);
		this.setVisible(true);
	}
	
	/**this method display copies and their details*/
	public void showCopies()
	{
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(41, 92, 461, 210);
		add(scrollPane);
		
		try {
			String s[]={"getCopies"};
			clc.client.sendToServer(s);
			while(clc.client.flag==0)
				System.out.print("");
			clc.client.flag=0;
			list=clc.client.details;
			table = new JTable(list.size(),2);
			table.getColumnModel().getColumn(0).setHeaderValue("Copy ID");
			table.getColumnModel().getColumn(1).setHeaderValue("Book ID");
			scrollPane.setViewportView(table);
			for(int i=0;i<list.size();i++)
			{
				for(int j=0;j<2;j++)
					table.setValueAt(list.get(i)[j],i,j);
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}
